﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Meraz.Core.Parsers
{
    public class MichelleMalkinParser : RegexBlogParser
    {
        public MichelleMalkinParser() { this.BlogKey = BlogKeyType.MichelleMalkin; }
        string rawDateString = null;
        string alias = null;
        const string bylinePattern = @"<div class=""author"">\s*By (?<ALIAS>[^&]+)[&nbsp;#826]+(?<DATE>.+(AM|PM))\s*</div>";
        internal void ParseByLine(string source){
            Match match = Regex.Match(source, bylinePattern,RegexOptions.IgnoreCase);
            if (!match.Success)
                return;
            alias = match.Groups["ALIAS"].Value;
            rawDateString = match.Groups["DATE"].Value;
        }
        internal override List<string> ParseAliases(string source)
        {
            if (string.IsNullOrWhiteSpace(alias))
                ParseByLine(source);
            return new List<string> { alias };
        }

        internal override DateTime ParseDate(string source)
        {
            if (string.IsNullOrWhiteSpace(rawDateString))
                ParseByLine(source);
            return DateTime.Parse(rawDateString);
        }

        internal override string ParseBody(string source)
        {
            string startMarker = @"<div class=""blog"">";
            int start = source.IndexOf(startMarker);
            if (start < 0)
                return null;
            start += startMarker.Length;
            int end = source.IndexOf(@"<div class=""postCategories"">", start);
            return source.Substring(start, end - start);
        }
    }
}
